/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rps.controller;

import bean.entity.Applicants;
import bean.entity.Employees;
import bean.session.ApplicantSessionBean;
import bean.session.InterviewSessionBean;
import bean.session.VacancySessionBean;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import bean.entity.Interviews;
import bean.entity.Vacances;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/**
 *
 * @author server
 */
@ManagedBean
@SessionScoped
public class InterviewsManagedBean {

    @EJB
    private VacancySessionBean vacancySessionBean;
    @EJB
    private ApplicantSessionBean applicantSessionBean;
    @EJB
    private InterviewSessionBean interviewSessionBean;
    private List<Interviews> listInterviews;
    private Interviews inter;
    private int count;
    private int param;
    private String p;
    private Employees em;
    private String names;
    private List<Applicants> listApplicant;
    private List<Vacances> listVacancy;
    private String name;
    private int select;
    private Applicants app;
    private int hired;
    private int banned;
    private int Hiredvacancy;
    private int Bannedvacancy;
    private int Inprocessvacancy;
    private int statusApp;

    /** Creates a new instance of InterviewsManagedBean */
    public InterviewsManagedBean() {
        inter = new Interviews();
        em = new Employees();
        listVacancy = new LinkedList<Vacances>();
        listApplicant = new LinkedList<Applicants>();
        app = new Applicants();

    }

    public String updateInterview() {
        applicantSessionBean.updateStatus(listInterviews);
        interviewSessionBean.updateInterViews(listInterviews);
        return "pretty:interviewer";
    }

    public String updateOneInterview() {

//        applicantSessionBean.updateOneStatus(inter.getApplicants().getApplicantID(), inter.getInStatus());
        interviewSessionBean.updateOneInterViews(inter);
        listApplicant = applicantSessionBean.findAllApplicant();
//        if (inter.getInStatus() == 2) {
//            interviewSessionBean.deleteInterviews(inter.getApplicants().getApplicantID());
//        }
        return "pretty:interviewer";
    }

    public String updateStatusApplicants() {

        if (statusApp == 3) {
            applicantSessionBean.updateOneStatus(inter.getApplicants().getApplicantID(), 3);
             interviewSessionBean.deleteInterviews(inter.getApplicants().getApplicantID());
        } else if (statusApp == 4) {
            applicantSessionBean.updateOneStatus(inter.getApplicants().getApplicantID(), 4);
        }
        statusApp = 0;
        return "pretty:hrgroup";
    }

    /**
     * @return the listInterviews
     */
    public List<Interviews> getListInterviews() {

        listApplicant = applicantSessionBean.findAllApplicant();
        return listInterviews;
    }

    public List<Interviews> getAllInterviews() {
        return interviewSessionBean.findAllreportInterviews();
    }

    /**
     * @param listInterviews the listInterviews to set
     */
    public void setListInterviews(List<Interviews> listInterviews) {
        this.listInterviews = listInterviews;
    }

    /**
     * @return the count
     */
    public String showInterviewsDetails(Interviews Inter) {
        this.inter = Inter;
        return "pretty:interviewsdetails";
    }

    public String showApplicantsDetails() {
        return "pretty:applicantinterviews";
    }

    public List<Interviews> getlistInterbyApplicant() {
        return interviewSessionBean.findAllbyApplicant();

    }

    public String detailsInterviewApplicants(Interviews Inter) {
        this.inter = Inter;
        return "pretty:detailsbyApplicants";
    }

    public String finInterdetails() {
        return "pretty:finInterdetails";
    }

    public String showReportDetails(Interviews Inter) {
        this.inter = Inter;
        listInterviews = null;
        Hiredvacancy = 0;
        Bannedvacancy = 0;
        Inprocessvacancy = 0;
        return "pretty:reportdetails";
    }

    public void findIDVancancy(ValueChangeEvent e) {
        UIInput nameInput = (UIInput) e.getComponent();
        String name = (String) nameInput.getValue();
        if (name != null && select == 1) {
            listVacancy = vacancySessionBean.findIdVacancy(name);
        } else {
            listVacancy = vacancySessionBean.findVacacyTitle(name);
        }

    }

    public void findIDApplicant(ValueChangeEvent e) {
        UIInput nameInput = (UIInput) e.getComponent();
        String name = (String) nameInput.getValue();
        if (name != null && select == 1) {
            listApplicant = applicantSessionBean.findIdapplicant(name);
        } else {
            listApplicant = applicantSessionBean.findApplicantName(name);
        }

    }

    public String addNewApplicant() {
        try {


            Date d = new Date();
            app.setApplicantID("");
            app.setDoc(d);
            app.setAppStatus(1);
            applicantSessionBean.addNewApplicant(app);
            listApplicant = applicantSessionBean.findAllApplicant();
            app.setApplicantName("");
            app.setAppPhone("");
            app.setAppEmail("");
            app.setAppAbout("");
            app.setDoc(null);
            app.setAppStatus(0);
        } catch (Exception e) {
            return null;
        }
        return "pretty:hrgroup";
    }

    public Applicants getApplicant() {
        return app;
    }

    public void setApplicant(Applicants ap) {
        this.app = ap;
    }

    public void setQuery(ValueChangeEvent e) {
        UIInput nameInput = (UIInput) e.getComponent();
        int ID = Integer.parseInt(nameInput.getValue().toString());
        select = ID;
    }

    public void report(ValueChangeEvent e) {
        UIInput nameInput = (UIInput) e.getComponent();
        String id = nameInput.getValue().toString();
        listInterviews = interviewSessionBean.findIdVacancyInterviews(id);
        Bannedvacancy = interviewSessionBean.totalVacancy(3, id);
        Hiredvacancy = interviewSessionBean.totalVacancy(2, id);
        Inprocessvacancy = interviewSessionBean.totalVacancy(0, id);
    }

    public List<SelectItem> getListItemInterviews() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        List<Vacances> in = vacancySessionBean.findAllVacances();
        items.add(new SelectItem(0, "---Select---"));
        if (in == null || in.size() > 0) {
            for (Vacances item : in) {
                items.add(new SelectItem(item.getVacancyID(), item.getVacantyTitle()));
            }
        }
        return items;
    }

    /**
     * @param count the count to set
     */
    public void setCount(int count) {
        this.count = count;
    }

    /**
     * @return the inter
     */
    public Interviews getInter() {
        return inter;
    }

    /**
     * @param inter the inter to set
     */
    public void setInter(Interviews inter) {
        this.inter = inter;
    }

    /**
     * @return the param
     */
    public int getParam() {
        return param;
    }

    /**
     * @param param the param to set
     */
    public void setParam(int param) {
        this.param = param;
    }

    /**
     * @return the p
     */
    public String getP() {
        return p;
    }

    /**
     * @param p the p to set
     */
    public void setP(String p) {
        this.p = p;
    }

    /**
     * @return the names
     */
    public String getNames() {
        return names;
    }

    /**
     * @param names the names to set
     */
    public void setNames(String names) {
        this.names = names;
    }

    /**
     * @return the listApplicant
     */
    public List<Applicants> getListApplicant() {
        if (listApplicant.size() <= 0) {
            return listApplicant = applicantSessionBean.findAllApplicant();
        } else {
            return listApplicant;
        }
    }

    public List<Interviews> getListHiredApplicants() {
        return interviewSessionBean.findByStatus(2);
    }

    public List<Interviews> getListBannedApplicants() {
        return interviewSessionBean.findByStatus(3);
    }

    public int getcountByVacancyID() {
        return interviewSessionBean.countByVacancyID();
    }

    /**
     * @param listApplicant the listApplicant to set
     */
    public void setListApplicant(List<Applicants> listApplicant) {
        this.listApplicant = listApplicant;
    }

    /**
     * @return the listVacancy
     */
    public List<Vacances> getListVacancy() {
        if (listVacancy.size() <= 0) {
            return listVacancy = vacancySessionBean.findAllVacances();
        } else {
            return listVacancy;
        }
    }

    /**
     * @param listVacancy the listVacancy to set
     */
    public void setListVacancy(List<Vacances> listVacancy) {
        this.listVacancy = listVacancy;
    }

    public Employees getEm() {
        return em;
    }

    public void setEm(Employees em) {
        this.em = em;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the select
     */
    public int getSelect() {
        return select;
    }

    /**
     * @param select the select to set
     */
    public void setSelect(int select) {
        this.select = select;
    }

    /**
     * @return the app
     */
    public Applicants getApp() {
        return app;
    }

    /**
     * @param app the app to set
     */
    public void setApp(Applicants app) {
        this.app = app;
    }

    public int getHired() {
        hired = interviewSessionBean.countStatusInterviews(2);
        return hired;
    }

    public void setHired(int hired) {
        this.hired = hired;
    }

    /**
     * @return the Banned
     */
    public int getBanned() {
        banned = interviewSessionBean.countStatusInterviews(3);
        return banned;
    }

    /**
     * @param Banned the Banned to set
     */
    public void setBanned(int banned) {
        this.banned = banned;
    }

    /**
     * @return the count
     */
    public int getCount() {
        return count;
    }

    /**
     * @return the Hiredvacancy
     */
    public int getHiredvacancy() {
        return Hiredvacancy;
    }

    /**
     * @param Hiredvacancy the Hiredvacancy to set
     */
    public void setHiredvacancy(int Hiredvacancy) {
        this.Hiredvacancy = Hiredvacancy;
    }

    /**
     * @return the Bannedvacancy
     */
    public int getBannedvacancy() {
        return Bannedvacancy;
    }

    /**
     * @param Bannedvacancy the Bannedvacancy to set
     */
    public void setBannedvacancy(int Bannedvacancy) {
        this.Bannedvacancy = Bannedvacancy;
    }

    /**
     * @return the Inprocessvacancy
     */
    public int getInprocessvacancy() {
        return Inprocessvacancy;
    }

    /**
     * @param Inprocessvacancy the Inprocessvacancy to set
     */
    public void setInprocessvacancy(int Inprocessvacancy) {
        this.Inprocessvacancy = Inprocessvacancy;
    }

    /**
     * @return the statusApp
     */
    public int getStatusApp() {
        return statusApp;
    }

    /**
     * @param statusApp the statusApp to set
     */
    public void setStatusApp(int statusApp) {
        this.statusApp = statusApp;
    }
}
